-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| const express = require("express"); | ||
| const axios = require("axios"); | ||
| const crypto = require("crypto"); | ||
| const cors = require("cors"); | ||
|
|
||
| const app = express(); | ||
| const PORT = 5000; | ||
| const AUTH_URL = "http://20.244.56.144/test/auth"; | ||
| const BASE_URL = "http://20.244.56.144/test/companies"; | ||
| const COMPANIES = ["AMZ", "FLP", "SNP", "MYN", "AZO"]; | ||
|
|
||
| const authData = { | ||
| companyName: "affordmed", | ||
| clientID: "546e0ea2-a7b3-4a93-9368-267ac7fd5729", | ||
| clientSecret: "hKiviNrTWjxPsVKo", | ||
| ownerName: "Mani Charan Reddy Gade", | ||
| ownerEmail: "21eg112b31@anurag.edu.in", | ||
| rollNo: "21EG112B31", | ||
| }; | ||
|
|
||
| let productCache = {}; | ||
| let bearerToken = null; | ||
| let tokenExpirationTime = 0; | ||
|
|
||
| const generateProductId = (product) => { | ||
| return crypto.createHash("md5").update(JSON.stringify(product)).digest("hex"); | ||
| }; | ||
|
|
||
| app.use(cors()); | ||
| app.use( | ||
| cors({ | ||
| origin: "http://localhost:3000", | ||
| }) | ||
| ); | ||
|
|
||
| const getBearerToken = async () => { | ||
| if (bearerToken && Date.now() < tokenExpirationTime) { | ||
| return bearerToken; | ||
| } | ||
|
|
||
| try { | ||
| const response = await axios.post(AUTH_URL, authData); | ||
| bearerToken = response.data.token; | ||
| tokenExpirationTime = Date.now() + 3600000; | ||
| return bearerToken; | ||
| } catch (error) { | ||
| console.error("Failed to fetch bearer token:", error); | ||
| throw error; | ||
| } | ||
| }; | ||
|
|
||
| app.get("/categories/:categoryname/products", async (req, res) => { | ||
| const { categoryname } = req.params; | ||
| const { | ||
| n = 10, | ||
| page = 1, | ||
| sort_by, | ||
| order = "asc", | ||
| minPrice = 1, | ||
| maxPrice = 10000, | ||
| } = req.query; | ||
|
|
||
| try { | ||
| const token = await getBearerToken(); | ||
|
|
||
| const requests = COMPANIES.map((company) => { | ||
| return axios.get( | ||
| `${BASE_URL}/${company}/categories/${categoryname}/products?top=${n}&minPrice=${minPrice}&maxPrice=${maxPrice}`, | ||
| { | ||
| headers: { | ||
| Authorization: `Bearer ${token}`, | ||
| }, | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| const responses = await Promise.all(requests); | ||
| let products = responses.flatMap((response) => response.data); | ||
|
|
||
| products = products.map((product) => { | ||
| const id = generateProductId(product); | ||
| productCache[id] = product; | ||
| return { ...product, id }; | ||
| }); | ||
|
|
||
| if (sort_by) { | ||
| products.sort((a, b) => { | ||
| if (order === "asc") { | ||
| return a[sort_by] > b[sort_by] ? 1 : -1; | ||
| } else { | ||
| return a[sort_by] < b[sort_by] ? 1 : -1; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| const startIndex = (page - 1) * n; | ||
| const paginatedProducts = products.slice(startIndex, startIndex + n); | ||
|
|
||
| res.json(paginatedProducts); | ||
| } catch (error) { | ||
| console.error(error); | ||
| res.status(500).json({ error: "Failed to fetch products" }); | ||
| } | ||
| }); | ||
|
|
||
| app.get("/categories/:categoryname/products/:productid", (req, res) => { | ||
| const { productid } = req.params; | ||
| const product = productCache[productid]; | ||
|
|
||
| if (product) { | ||
| res.json(product); | ||
| } else { | ||
| res.status(404).json({ error: "Product not found" }); | ||
| } | ||
| }); | ||
|
|
||
| app.listen(PORT, () => { | ||
| console.log(`Server running on http://localhost:${PORT}`); | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "name": "server", | ||
| "version": "1.0.0", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "description": "", | ||
| "dependencies": { | ||
| "axios": "^1.7.5", | ||
| "cors": "^2.8.5", | ||
| "crypto": "^1.0.1", | ||
| "express": "^4.19.2" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
|
||
| # dependencies | ||
| /node_modules | ||
| /.pnp | ||
| .pnp.js | ||
|
|
||
| # testing | ||
| /coverage | ||
|
|
||
| # production | ||
| /build | ||
|
|
||
| # misc | ||
| .DS_Store | ||
| .env.local | ||
| .env.development.local | ||
| .env.test.local | ||
| .env.production.local | ||
|
|
||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # Getting Started with Create React App | ||
|
|
||
| This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). | ||
|
|
||
| ## Available Scripts | ||
|
|
||
| In the project directory, you can run: | ||
|
|
||
| ### `npm start` | ||
|
|
||
| Runs the app in the development mode.\ | ||
| Open [http://localhost:3000](http://localhost:3000) to view it in your browser. | ||
|
|
||
| The page will reload when you make changes.\ | ||
| You may also see any lint errors in the console. | ||
|
|
||
| ### `npm test` | ||
|
|
||
| Launches the test runner in the interactive watch mode.\ | ||
| See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. | ||
|
|
||
| ### `npm run build` | ||
|
|
||
| Builds the app for production to the `build` folder.\ | ||
| It correctly bundles React in production mode and optimizes the build for the best performance. | ||
|
|
||
| The build is minified and the filenames include the hashes.\ | ||
| Your app is ready to be deployed! | ||
|
|
||
| See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. | ||
|
|
||
| ### `npm run eject` | ||
|
|
||
| **Note: this is a one-way operation. Once you `eject`, you can't go back!** | ||
|
|
||
| If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. | ||
|
|
||
| Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own. | ||
|
|
||
| You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it. | ||
|
|
||
| ## Learn More | ||
|
|
||
| You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). | ||
|
|
||
| To learn React, check out the [React documentation](https://reactjs.org/). | ||
|
|
||
| ### Code Splitting | ||
|
|
||
| This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) | ||
|
|
||
| ### Analyzing the Bundle Size | ||
|
|
||
| This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) | ||
|
|
||
| ### Making a Progressive Web App | ||
|
|
||
| This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) | ||
|
|
||
| ### Advanced Configuration | ||
|
|
||
| This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) | ||
|
|
||
| ### Deployment | ||
|
|
||
| This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) | ||
|
|
||
| ### `npm run build` fails to minify | ||
|
|
||
| This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| { | ||
| "name": "client", | ||
| "version": "0.1.0", | ||
| "private": true, | ||
| "dependencies": { | ||
| "@testing-library/jest-dom": "^5.17.0", | ||
| "@testing-library/react": "^13.4.0", | ||
| "@testing-library/user-event": "^13.5.0", | ||
| "axios": "^1.7.5", | ||
| "react": "^18.3.1", | ||
| "react-dom": "^18.3.1", | ||
| "react-router-dom": "^6.26.1", | ||
| "react-scripts": "5.0.1", | ||
| "react-toastify": "^10.0.5", | ||
| "styled-components": "^6.1.12", | ||
| "web-vitals": "^2.1.4" | ||
| }, | ||
| "scripts": { | ||
| "start": "react-scripts start", | ||
| "build": "react-scripts build", | ||
| "test": "react-scripts test", | ||
| "eject": "react-scripts eject" | ||
| }, | ||
| "eslintConfig": { | ||
| "extends": [ | ||
| "react-app", | ||
| "react-app/jest" | ||
| ] | ||
| }, | ||
| "browserslist": { | ||
| "production": [ | ||
| ">0.2%", | ||
| "not dead", | ||
| "not op_mini all" | ||
| ], | ||
| "development": [ | ||
| "last 1 chrome version", | ||
| "last 1 firefox version", | ||
| "last 1 safari version" | ||
| ] | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| <meta name="theme-color" content="#000000" /> | ||
| <meta | ||
| name="description" | ||
| content="Web site created using create-react-app" | ||
| /> | ||
| <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" /> | ||
| <!-- | ||
| manifest.json provides metadata used when your web app is installed on a | ||
| user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/ | ||
| --> | ||
| <link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> | ||
| <!-- | ||
| Notice the use of %PUBLIC_URL% in the tags above. | ||
| It will be replaced with the URL of the `public` folder during the build. | ||
| Only files inside the `public` folder can be referenced from the HTML. | ||
| Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will | ||
| work correctly both with client-side routing and a non-root public URL. | ||
| Learn how to configure a non-root public URL by running `npm run build`. | ||
| --> | ||
| <title>React App</title> | ||
| </head> | ||
| <body> | ||
| <noscript>You need to enable JavaScript to run this app.</noscript> | ||
| <div id="root"></div> | ||
| <!-- | ||
| This HTML file is a template. | ||
| If you open it directly in the browser, you will see an empty page. | ||
| You can add webfonts, meta tags, or analytics to this file. | ||
| The build step will place the bundled scripts into the <body> tag. | ||
| To begin the development, run `npm start` or `yarn start`. | ||
| To create a production bundle, use `npm run build` or `yarn build`. | ||
| --> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "short_name": "React App", | ||
| "name": "Create React App Sample", | ||
| "icons": [ | ||
| { | ||
| "src": "favicon.ico", | ||
| "sizes": "64x64 32x32 24x24 16x16", | ||
| "type": "image/x-icon" | ||
| }, | ||
| { | ||
| "src": "logo192.png", | ||
| "type": "image/png", | ||
| "sizes": "192x192" | ||
| }, | ||
| { | ||
| "src": "logo512.png", | ||
| "type": "image/png", | ||
| "sizes": "512x512" | ||
| } | ||
| ], | ||
| "start_url": ".", | ||
| "display": "standalone", | ||
| "theme_color": "#000000", | ||
| "background_color": "#ffffff" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # https://www.robotstxt.org/robotstxt.html | ||
| User-agent: * | ||
| Disallow: |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| .App { | ||
| text-align: center; | ||
| } | ||
|
|
||
| .App-logo { | ||
| height: 40vmin; | ||
| pointer-events: none; | ||
| } | ||
|
|
||
| @media (prefers-reduced-motion: no-preference) { | ||
| .App-logo { | ||
| animation: App-logo-spin infinite 20s linear; | ||
| } | ||
| } | ||
|
|
||
| .App-header { | ||
| background-color: #282c34; | ||
| min-height: 100vh; | ||
| display: flex; | ||
| flex-direction: column; | ||
| align-items: center; | ||
| justify-content: center; | ||
| font-size: calc(10px + 2vmin); | ||
| color: white; | ||
| } | ||
|
|
||
| .App-link { | ||
| color: #61dafb; | ||
| } | ||
|
|
||
| @keyframes App-logo-spin { | ||
| from { | ||
| transform: rotate(0deg); | ||
| } | ||
| to { | ||
| transform: rotate(360deg); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { BrowserRouter as Router, Route, Routes } from "react-router-dom"; | ||
| import AllProductsPage from "./pages/AllProductPage"; | ||
| import ProductDetailPage from "./pages/ProductDetailPage"; | ||
| import { createGlobalStyle, ThemeProvider } from 'styled-components'; | ||
|
|
||
| const GlobalStyle = createGlobalStyle` | ||
| body { | ||
| font-family: 'Roboto', sans-serif; | ||
| margin: 0; | ||
| padding: 0; | ||
| background-color: ${props => props.theme.colors.background}; | ||
| color: ${props => props.theme.colors.text}; | ||
| } | ||
| `; | ||
|
|
||
| const theme = { | ||
| colors: { | ||
| primary: '#3498db', | ||
| secondary: '#2ecc71', | ||
| background: '#f4f4f4', | ||
| text: '#333', | ||
| lightText: '#777', | ||
| border: '#ddd', | ||
| }, | ||
| breakpoints: { | ||
| mobile: '768px', | ||
| } | ||
| }; | ||
|
|
||
| function App() { | ||
| return ( | ||
| <ThemeProvider theme={theme}> | ||
| <GlobalStyle /> | ||
| <Router> | ||
| <Routes> | ||
| <Route path="/" element={<AllProductsPage />} /> | ||
| <Route path="/product/:productId" element={<ProductDetailPage />} /> | ||
| </Routes> | ||
| </Router> | ||
| </ThemeProvider> | ||
| ); | ||
| } | ||
|
|
||
| export default App; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import App from './App'; | ||
|
|
||
| test('renders learn react link', () => { | ||
| render(<App />); | ||
| const linkElement = screen.getByText(/learn react/i); | ||
| expect(linkElement).toBeInTheDocument(); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import React from "react"; | ||
| import styled from "styled-components"; | ||
|
|
||
| const FilterContainer = styled.div` | ||
| display: flex; | ||
| flex-wrap: wrap; | ||
| gap: 10px; | ||
| margin-bottom: 20px; | ||
| `; | ||
|
|
||
| const Select = styled.select` | ||
| padding: 8px; | ||
| border: 1px solid ${(props) => props.theme.colors.border}; | ||
| border-radius: 4px; | ||
| background-color: white; | ||
| font-size: 1rem; | ||
| `; | ||
|
|
||
| const Input = styled.input` | ||
| padding: 8px; | ||
| border: 1px solid ${(props) => props.theme.colors.border}; | ||
| border-radius: 4px; | ||
| font-size: 1rem; | ||
| `; | ||
|
|
||
| const FilterBar = ({ filters, setFilters, setSortBy, setOrder }) => { | ||
| const validCategories = [ | ||
| "Phone", | ||
| "Computer", | ||
| "TV", | ||
| "Earphone", | ||
| "Tablet", | ||
| "Charger", | ||
| "Mouse", | ||
| "Keypad", | ||
| "Bluetooth", | ||
| "Pendrive", | ||
| "Remote", | ||
| "Speaker", | ||
| "Headset", | ||
| "Laptop", | ||
| "PC", | ||
| ]; | ||
| const validCompanies = ["AMZ", "FLP", "SNP", "MYN", "AZO"]; | ||
|
|
||
| return ( | ||
| <FilterContainer> | ||
| <Select | ||
| value={filters.category} | ||
| onChange={(e) => setFilters({ ...filters, category: e.target.value })} | ||
| > | ||
| {validCategories.map((category) => ( | ||
| <option key={category} value={category}> | ||
| {category} | ||
| </option> | ||
| ))} | ||
| </Select> | ||
|
|
||
| <Select | ||
| value={filters.company} | ||
| onChange={(e) => setFilters({ ...filters, company: e.target.value })} | ||
| > | ||
| <option value="">All Companies</option> | ||
| {validCompanies.map((company) => ( | ||
| <option key={company} value={company}> | ||
| {company} | ||
| </option> | ||
| ))} | ||
| </Select> | ||
|
|
||
| <select | ||
| value={filters.rating} | ||
| onChange={(e) => | ||
| setFilters({ ...filters, rating: Number(e.target.value) }) | ||
| } | ||
| > | ||
| <option value={0}>All Ratings</option> | ||
| <option value={4}>4 Stars & Up</option> | ||
| <option value={3}>3 Stars & Up</option> | ||
| </select> | ||
|
|
||
| <Input | ||
| type="number" | ||
| placeholder="Min Price" | ||
| value={filters.minPrice} | ||
| onChange={(e) => | ||
| setFilters({ ...filters, minPrice: Number(e.target.value) }) | ||
| } | ||
| /> | ||
| <Input | ||
| type="number" | ||
| placeholder="Max Price" | ||
| value={filters.maxPrice} | ||
| onChange={(e) => | ||
| setFilters({ ...filters, maxPrice: Number(e.target.value) }) | ||
| } | ||
| /> | ||
|
|
||
| <Select onChange={(e) => setSortBy(e.target.value)}> | ||
| <option value="price">Sort by Price</option> | ||
| <option value="rating">Sort by Rating</option> | ||
| <option value="discount">Sort by Discount</option> | ||
| </Select> | ||
|
|
||
| <Select onChange={(e) => setOrder(e.target.value)}> | ||
| <option value="asc">Ascending</option> | ||
| <option value="desc">Descending</option> | ||
| </Select> | ||
| </FilterContainer> | ||
| ); | ||
| }; | ||
|
|
||
| export default FilterBar; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import React from "react"; | ||
| import styled from "styled-components"; | ||
|
|
||
| const PaginationContainer = styled.div` | ||
| display: flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
| margin-top: 20px; | ||
| `; | ||
|
|
||
| const PageButton = styled.button` | ||
| padding: 8px 16px; | ||
| margin: 0 5px; | ||
| border: none; | ||
| background-color: ${(props) => props.theme.colors.primary}; | ||
| color: white; | ||
| border-radius: 4px; | ||
| cursor: pointer; | ||
| &:disabled { | ||
| background-color: ${(props) => props.theme.colors.lightText}; | ||
| cursor: not-allowed; | ||
| } | ||
| `; | ||
|
|
||
| const PageInfo = styled.div` | ||
| margin: 0 10px; | ||
| font-size: 1rem; | ||
| `; | ||
|
|
||
| const Pagination = ({ page, setPage, totalPages }) => { | ||
| return ( | ||
| <PaginationContainer> | ||
| <PageButton onClick={() => setPage(page - 1)} disabled={page === 1}> | ||
| Previous | ||
| </PageButton> | ||
| <PageInfo> | ||
| {page} of {totalPages} | ||
| </PageInfo> | ||
| <PageButton | ||
| onClick={() => setPage(page + 1)} | ||
| disabled={page === totalPages} | ||
| > | ||
| Next | ||
| </PageButton> | ||
| </PaginationContainer> | ||
| ); | ||
| }; | ||
|
|
||
| export default Pagination; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import React from "react"; | ||
| import styled from "styled-components"; | ||
| import { Link } from "react-router-dom"; | ||
|
|
||
| const Card = styled(Link)` | ||
| display: block; | ||
| background-color: white; | ||
| border-radius: 8px; | ||
| overflow: hidden; | ||
| box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); | ||
| transition: transform 0.2s; | ||
| text-decoration: none; | ||
| color: ${(props) => props.theme.colors.text}; | ||
| &:hover { | ||
| transform: translateY(-5px); | ||
| } | ||
| `; | ||
|
|
||
| const CardImage = styled.img` | ||
| width: 100%; | ||
| height: 200px; | ||
| object-fit: cover; | ||
| `; | ||
|
|
||
| const CardContent = styled.div` | ||
| padding: 15px; | ||
| `; | ||
|
|
||
| const CardTitle = styled.h2` | ||
| font-size: 1.2rem; | ||
| margin-bottom: 10px; | ||
| `; | ||
|
|
||
| const CardInfo = styled.p` | ||
| font-size: 1rem; | ||
| color: ${(props) => props.theme.colors.lightText}; | ||
| margin-bottom: 5px; | ||
| `; | ||
|
|
||
| const ProductCard = ({ product }) => { | ||
| const randomImageUrl = `https://picsum.photos/seed/${product.id}/300/200`; | ||
|
|
||
| return ( | ||
| <Card to={`/product/${product.id}`}> | ||
| <CardImage src={randomImageUrl} alt={product.name} /> | ||
| <CardContent> | ||
| <CardTitle>{product.name}</CardTitle> | ||
| <CardInfo>Price: Rs.{product.price}</CardInfo> | ||
| <CardInfo>Rating: {product.rating}</CardInfo> | ||
| </CardContent> | ||
| </Card> | ||
| ); | ||
| }; | ||
|
|
||
| export default ProductCard; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| body { | ||
| margin: 0; | ||
| font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', | ||
| 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', | ||
| sans-serif; | ||
| -webkit-font-smoothing: antialiased; | ||
| -moz-osx-font-smoothing: grayscale; | ||
| } | ||
|
|
||
| code { | ||
| font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', | ||
| monospace; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import React from 'react'; | ||
| import ReactDOM from 'react-dom/client'; | ||
| import './index.css'; | ||
| import App from './App'; | ||
| import reportWebVitals from './reportWebVitals'; | ||
|
|
||
| const root = ReactDOM.createRoot(document.getElementById('root')); | ||
| root.render( | ||
| <React.StrictMode> | ||
| <App /> | ||
| </React.StrictMode> | ||
| ); | ||
|
|
||
| // If you want to start measuring performance in your app, pass a function | ||
| // to log results (for example: reportWebVitals(console.log)) | ||
| // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals | ||
| reportWebVitals(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import React, { useState, useEffect } from "react"; | ||
| import axios from "axios"; | ||
| import ProductCard from "../components/ProductCard"; | ||
| import FilterBar from "../components/FilterBar"; | ||
| import Pagination from "../components/Pagination"; | ||
| import styled from "styled-components"; | ||
|
|
||
| const PageContainer = styled.div` | ||
| max-width: 1200px; | ||
| margin: 0 auto; | ||
| padding: 20px; | ||
| `; | ||
|
|
||
| const Title = styled.h1` | ||
| font-size: 2.5rem; | ||
| color: ${(props) => props.theme.colors.primary}; | ||
| margin-bottom: 20px; | ||
| `; | ||
|
|
||
| const ProductGrid = styled.div` | ||
| display: grid; | ||
| grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); | ||
| gap: 20px; | ||
| `; | ||
|
|
||
| const AllProductsPage = () => { | ||
| const [products, setProducts] = useState([]); | ||
| const [filters, setFilters] = useState({ | ||
| category: "Laptop", | ||
| company: "", | ||
| rating: 0, | ||
| minPrice: 1, | ||
| maxPrice: 10000, | ||
| availability: "all", | ||
| }); | ||
| const [sortBy, setSortBy] = useState("price"); | ||
| const [order, setOrder] = useState("asc"); | ||
| const [page, setPage] = useState(1); | ||
| const [totalPages, setTotalPages] = useState(1); | ||
|
|
||
| useEffect(() => { | ||
| fetchProducts(); | ||
| }, [filters, sortBy, order, page]); | ||
|
|
||
| const fetchProducts = async () => { | ||
| const { category, minPrice, maxPrice } = filters; | ||
| try { | ||
| const response = await axios.get( | ||
| `http://localhost:5000/categories/${category}/products`, | ||
| { | ||
| params: { n: 10, page, sort_by: sortBy, order, minPrice, maxPrice }, | ||
| } | ||
| ); | ||
| setProducts(response.data); | ||
| setTotalPages(Math.ceil(response.data.length / 10)); | ||
| } catch (error) { | ||
| console.error("Failed to fetch products:", error); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <PageContainer> | ||
| <Title>All Products</Title> | ||
| <FilterBar | ||
| filters={filters} | ||
| setFilters={setFilters} | ||
| setSortBy={setSortBy} | ||
| setOrder={setOrder} | ||
| /> | ||
| <ProductGrid> | ||
| {products.map((product) => ( | ||
| <ProductCard key={product.id} product={product} /> | ||
| ))} | ||
| </ProductGrid> | ||
| <Pagination page={page} setPage={setPage} totalPages={totalPages} /> | ||
| </PageContainer> | ||
| ); | ||
| }; | ||
|
|
||
| export default AllProductsPage; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import React, { useEffect, useState } from "react"; | ||
| import { useParams } from "react-router-dom"; | ||
| import axios from "axios"; | ||
| import styled from "styled-components"; | ||
| const DetailContainer = styled.div` | ||
| max-width: 800px; | ||
| margin: 0 auto; | ||
| padding: 20px; | ||
| `; | ||
|
|
||
| const ProductTitle = styled.h1` | ||
| font-size: 2rem; | ||
| color: ${(props) => props.theme.colors.primary}; | ||
| margin-bottom: 20px; | ||
| `; | ||
|
|
||
| const ProductImage = styled.img` | ||
| width: 100%; | ||
| height: auto; | ||
| border-radius: 8px; | ||
| margin-bottom: 20px; | ||
| `; | ||
|
|
||
| const ProductInfo = styled.p` | ||
| font-size: 1.1rem; | ||
| margin-bottom: 10px; | ||
| &.bold { | ||
| font-weight: bold; | ||
| } | ||
| `; | ||
|
|
||
| const ProductDetailPage = () => { | ||
| const { productId } = useParams(); | ||
| const [product, setProduct] = useState(null); | ||
|
|
||
| useEffect(() => { | ||
| fetchProduct(); | ||
| }, [productId]); | ||
|
|
||
| const fetchProduct = async () => { | ||
| try { | ||
| const response = await axios.get( | ||
| `http://localhost:5000/categories/Laptop/products/${productId}` | ||
| ); | ||
| setProduct(response.data); | ||
| } catch (error) { | ||
| console.error("Failed to fetch product:", error); | ||
| } | ||
| }; | ||
|
|
||
| if (!product) return <div>Loading...</div>; | ||
|
|
||
| const randomImageUrl = `https://picsum.photos/seed/${product.id}/600/400`; | ||
|
|
||
| return ( | ||
| <DetailContainer> | ||
| <ProductTitle>{product.name}</ProductTitle> | ||
| <ProductImage src={randomImageUrl} alt={product.name} /> | ||
| <ProductInfo className="bold">Price: Rs.{product.price}</ProductInfo> | ||
| <ProductInfo className="bold">Rating: {product.rating}</ProductInfo> | ||
| <ProductInfo className="bold">Discount: {product.discount}%</ProductInfo> | ||
| <ProductInfo className="bold"> | ||
| Availability: {product.availability} | ||
| </ProductInfo> | ||
| </DetailContainer> | ||
| ); | ||
| }; | ||
|
|
||
| export default ProductDetailPage; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| const reportWebVitals = onPerfEntry => { | ||
| if (onPerfEntry && onPerfEntry instanceof Function) { | ||
| import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { | ||
| getCLS(onPerfEntry); | ||
| getFID(onPerfEntry); | ||
| getFCP(onPerfEntry); | ||
| getLCP(onPerfEntry); | ||
| getTTFB(onPerfEntry); | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| export default reportWebVitals; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| // jest-dom adds custom jest matchers for asserting on DOM nodes. | ||
| // allows you to do things like: | ||
| // expect(element).toHaveTextContent(/react/i) | ||
| // learn more: https://github.com/testing-library/jest-dom | ||
| import '@testing-library/jest-dom'; |